package 可重入锁.lock的不同方式.tryLock不带参数;

import java.util.concurrent.locks.ReentrantLock;

public class TryLock implements Runnable {
    private static ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        if (lock.tryLock()) {
            System.out.println(Thread.currentThread().getName() + " 得到锁");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else{
            System.out.println(Thread.currentThread().getName() + " 没得到锁");
        }
    }
/*
    tryLock()没有得到锁，不等待，直接返回false
 */
    public static void main(String[] args) {
        TryLock lock = new TryLock();
        Thread thread1 = new Thread(lock,"第一个线程");
        Thread thread2 = new Thread(lock,"第二个线程");
        thread1.start();
        thread2.start();
    }
    //第一个线程 得到锁
    //第二个线程 没得到锁
}
